package com.jinzhi.jzweb.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzapi.common.controller.ApittController;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.pojo.ExaminationAnswerSheetComBExlVO;
import com.jinzhi.jzweb.pojo.ExaminationAnswerSheetComExlVO;
import com.jinzhi.jzweb.pojo.ExaminationAnswerSheetComVO;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.jzweb.utils.DoubleUtil;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import com.jinzhi.jzweb.utils.ImgMp4ValueUtil;
import com.jinzhi.sys.service.UserRoleService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * 超级管理员 总裁篇查看分数
 * <pre>
 * B赛场查看分数 统计
 * 2023
 * </pre>
 * <small> 2023-05-05 15:11:09 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/examinationAnswerSheetComBSour")
public class ExaminationAnswerSheetComBSourController extends BaseController {
    @Autowired
    private ExaminationAnswerSheetComService examinationAnswerSheetComService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private ExaminationAnswerSheetComService examinationAnswerSheetService;
    @Autowired
    private ExaminationPaperService examinationPaperService;
    @Autowired
    private StuSignUpComService stuSignUpService;
    @Autowired
    private ExaminationQuestionsService examinationQuestionsService;
    @Autowired
    private ExaminationBatchService examinationBatchService;
    @Autowired
    private ExaminationBatchComSService comSService;
    @Autowired
    private ExaminationResultsService resultsService;
    @Autowired
    private ExaminationResultsComService examinationResultsService;
    @Autowired
    private ApittController apittController;
    @Autowired
    private  ExaminationResultsImgMp4ComService imgMp4ComService;
    /**
     * B赛场成绩统计导出
     *
     * @param response
     */
    @GetMapping("/downloadImportStu")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:examinationAnswerSheetComSour")
    @ResponseBody
    public void downloadImportTemplate(HttpServletResponse response, ExaminationResultsComDO examinationAnswerSheetComDT) {
        List<ExaminationAnswerSheetComBExlVO> list1 = new LinkedList<>();
        Wrapper<StuSignUpComDO> wrapper=new EntityWrapper<>();

        if(StringUtils.isNotBlank(examinationAnswerSheetComDT.getUnitName())){
            wrapper.like("unit_name",examinationAnswerSheetComDT.getUnitName());
        }
        // 查询列表数据
        Page<StuSignUpComDO> list = stuSignUpService.selectPage(getPage(StuSignUpComDO.class),
                wrapper.orderBy("id", false));
        for(StuSignUpComDO stuSignUpComDO:list.getRecords()){
            ExaminationResultsComDO examinationResultsComDO=  examinationResultsService.findOneByKv("stu_sign_id",stuSignUpComDO.getId());
            if(examinationResultsComDO!=null){
                ExaminationResultsComDO obj = new ExaminationResultsComDO();
                ExaminationResultsComResourcesDO img= imgMp4ComService.findOneByKv("results_id",examinationResultsComDO.getId());
                if(img!=null){
                    obj= ImgMp4ValueUtil.testRear(examinationResultsComDO,img);
                }else {
                    obj = examinationResultsComDO;
                }
                ExaminationAnswerSheetComBExlVO exlVO=new ExaminationAnswerSheetComBExlVO();
               // exlVO.setNum2(stuSignUpComDO.getNum2());
                ExaminationBatchComSDO examinationBatchComSDO= comSService.selectById(stuSignUpComDO.getCourt2());
                if(examinationBatchComSDO!=null){
                    exlVO.setNum2(examinationBatchComSDO.getName()+"-"+stuSignUpComDO.getRealityNm2());
                }else {
                    //  obj.setNum2s(stuSignUpComDO.getRealityNm2().toString());
                    exlVO.setNum2(stuSignUpComDO.getRealityNm2()!=null?stuSignUpComDO.getRealityNm2().toString():null);

                }

                exlVO.setName(stuSignUpComDO.getName());
                exlVO.setUnitName(stuSignUpComDO.getUnitName());
                exlVO.setIdNumber(stuSignUpComDO.getIdNumber());
               // exlVO.setTicketNumber(stuSignUpComDO.getTicketNumber());
                // Double sour=obj.getTotalScore();
                if(obj.getScoingb1()==null){
                    obj.setScoingb1(0.0);
                }
                if(obj.getScoingb2()==null){
                    obj.setScoingb2(0.0);
                }
                if(obj.getScoingb3()==null){
                    obj.setScoingb3(0.0);
                }
                if(obj.getScoingb4()==null){
                    obj.setScoingb4(0.0);
                }
                if(obj.getScoingb5()==null){
                    obj.setScoingb5(0.0);
                }


                if(obj.getScoingb1s()==null){
                    obj.setScoingb1s(0.0);
                }
                if(obj.getScoingb2s()==null){
                    obj.setScoingb2s(0.0);
                }
                if(obj.getScoingb3s()==null){
                    obj.setScoingb3s(0.0);
                }
                if(obj.getScoingb4s()==null){
                    obj.setScoingb4s(0.0);
                }
                if(obj.getScoingb5s()==null){
                    obj.setScoingb5s(0.0);
                }

                double s11=(obj.getScoingb1()*0.3);
                double f11 = DoubleUtil.douToTwo(s11);

                double s11s=(obj.getScoingb1s()*0.2);
                double f11s = DoubleUtil.douToTwo(s11s);
                String a=String.valueOf(f11)+"/"+(f11s);

                double s12=(obj.getScoingb2()*0.3);
                double f12 = DoubleUtil.douToTwo(s12);

                double s12s=(obj.getScoingb2s()*0.2);
                double f12s = DoubleUtil.douToTwo(s12s);
                String b=String.valueOf(f12)+"/"+(f12s);


                double s13=(obj.getScoingb3()*0.3);
                double f13 = DoubleUtil.douToTwo(s13);


                double s13s=(obj.getScoingb3s()*0.2);
                double f13s = DoubleUtil.douToTwo(s13s);
                String c=String.valueOf(f13)+"/"+(f13s);

                exlVO.setScore1(String.valueOf(a));
                exlVO.setScore2(b);
                exlVO.setScore3(c);
               // exlVO.setScore4(d);
               // exlVO.setScore5(e);
                double s1=((obj.getScoingb1()+obj.getScoingb2()+obj.getScoingb3())*0.3)/3;
                double s2=((obj.getScoingb1s()+obj.getScoingb2s()+obj.getScoingb3s())*0.2)/3;
                BigDecimal bg = new BigDecimal(s1+s2);
                double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

                exlVO.setScore(String.valueOf(f1));

                list1.add(exlVO);
            }else {
                ExaminationAnswerSheetComBExlVO obj = new ExaminationAnswerSheetComBExlVO();
                obj.setNum2(stuSignUpComDO.getNum2()!=null?stuSignUpComDO.getNum2().toString():null);
                obj.setName(stuSignUpComDO.getName());
                obj.setUnitName(stuSignUpComDO.getUnitName());
                obj.setIdNumber(stuSignUpComDO.getIdNumber());
               // obj.setTicketNumber(stuSignUpComDO.getTicketNumber());
//                obj.setScoingb1(0.0);
//                obj.setScoingb2(0.0);
//                obj.setScoingb3(0.0);
                obj.setScore("0.0");
                obj.setScore1("0/0");
                obj.setScore2("0/0");
                obj.setScore3("0/0");
                list1.add(obj);

            }
            // list1.add(stuSignUpComVo);
        }

        //导出操作
        EasyPoiUtil.exportExcel(list1, "B赛场成绩导出", "B赛场成绩导出", ExaminationAnswerSheetComBExlVO.class, "B赛场成绩导出", response);
    }



    /**
     * 超级管理员 一级管理员查看分数
     * @param model
     * @return
     */
    @GetMapping()
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:examinationAnswerSheetComSour")
    String info( Model model) {
        return "jzweb/examinationAnswerSheetCom/examinationAnswerSheetComBSour";
    }



    @ApiOperation(value = "超级管理员 一级管理员查看分数（B赛场，安全防护赛场）页面入口", notes = "超级管理员 一级管理员查看分数（B赛场，安全防护赛场）")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping(value = "/getAnswerSheetComBsecure")
    @RequiresPermissions("jzweb:examinationBnswerSheetCom:answerSheetComBsecure")
    public String moduleEntrance(){
		return "jzweb/examinationAnswerSheetCom/answerSheetComBsecure";
    }



    @ApiOperation(value = "超级管理员 一级管理员查看分数（B赛场，日常保健赛场）页面入口", notes = "超级管理员 一级管理员查看分数（B赛场，日常保健赛场）")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping(value = "/getAnswerSheetComHealth")
    @RequiresPermissions("jzweb:examinationBnswerSheetCom:answerSheetComBHealth")
    public String moduleHealthEntrance(){
        return "jzweb/examinationAnswerSheetCom/answerSheetcomBhealth";
    }



    @ApiOperation(value = "超级管理员 一级管理员查看分数（C赛场，生活照料赛场）页面入口", notes = "超级管理员 一级管理员查看分数（C赛场，生活照料赛场）")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping(value = "/getAnswerSheetComLookAfter")
    @RequiresPermissions("jzweb:examinationBnswerSheetCom:answerSheetComClookAfter")
    public String moduleLookAfterEntrance(){
        return "jzweb/examinationAnswerSheetCom/answerSheetComClookAfter";
    }




    @ApiOperation(value = "超级管理员 一级管理员查看分数（C赛场，早期发展指导赛场）页面入口", notes = "超级管理员 一级管理员查看分数（C赛场，早期发展指导赛场）")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping(value = "/getAnswerSheetComdeveLopmentGuidance")
    @RequiresPermissions("jzweb:examinationBnswerSheetCom:answerSheetComClopmentGuidance")
    public String moduleLopmentGuidanceEntrance(){
        return "jzweb/examinationAnswerSheetCom/answerSheetComClopmentGuidance";
    }




    /**
     * B赛场理论成绩统计
     * @param examinationAnswerSheetComDTO
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:examinationAnswerSheetComSour")
    public Result<Page<StuSignUpComDO>> list(ExaminationResultsComDO examinationAnswerSheetComDTO){

        Wrapper<StuSignUpComDO> wrapper=new EntityWrapper<>();

        if(StringUtils.isNotBlank(examinationAnswerSheetComDTO.getUnitName())){
            wrapper.like("unit_name",examinationAnswerSheetComDTO.getUnitName());
        }
        // 查询列表数据
        Page<StuSignUpComDO> page = stuSignUpService.selectPage(getPage(StuSignUpComDO.class),

                wrapper.orderBy("id", false));


        List list = new ArrayList();
        for (int i = 0; i < page.getRecords().size(); i++) {
            ExaminationResultsComDO examinationResultsComDO=  examinationResultsService.findOneByKv("stu_sign_id",page.getRecords().get(i).getId());
            StuSignUpComDO stuSignUpComDO=page.getRecords().get(i);
            if(examinationResultsComDO!=null){
                ExaminationResultsComDO obj = new ExaminationResultsComDO();
                ExaminationResultsComResourcesDO img= imgMp4ComService.findOneByKv("results_id",examinationResultsComDO.getId());
                if(img!=null){
                    obj= ImgMp4ValueUtil.testRear(examinationResultsComDO,img);
                }else {
                    obj = examinationResultsComDO;
                }
                ExaminationBatchComSDO examinationBatchComSDO= comSService.selectById(page.getRecords().get(i).getCourt2());
                if(examinationBatchComSDO!=null){
                    obj.setNum2s(examinationBatchComSDO.getName()+"-"+stuSignUpComDO.getRealityNm2());
                }else {
                  //  obj.setNum2s(stuSignUpComDO.getRealityNm2().toString());
                    obj.setNum2s(stuSignUpComDO.getRealityNm2()!=null?stuSignUpComDO.getRealityNm2().toString():null);

                }

                obj.setName(stuSignUpComDO.getName());
                obj.setUnitName(stuSignUpComDO.getUnitName());
                obj.setIdNumber(stuSignUpComDO.getIdNumber());
                obj.setTicketNumber(stuSignUpComDO.getTicketNumber());
               // Double sour=obj.getTotalScore();
                if(obj.getScoingb1()==null){
                    obj.setScoingb1(0.0);
                }
                if(obj.getScoingb2()==null){
                    obj.setScoingb2(0.0);
                }
                if(obj.getScoingb3()==null){
                    obj.setScoingb3(0.0);
                }


                if(obj.getScoingb1s()==null){
                    obj.setScoingb1s(0.0);
                }
                if(obj.getScoingb2s()==null){
                    obj.setScoingb2s(0.0);
                }
                if(obj.getScoingb3s()==null){
                    obj.setScoingb3s(0.0);
                }


                double s11=(obj.getScoingb1()*0.3);
                double f11 = DoubleUtil.douToTwo(s11);

                double s11s=(obj.getScoingb1s()*0.2);
                double f11s = DoubleUtil.douToTwo(s11s);
                String a=String.valueOf(f11)+"/"+(f11s);

                double s12=(obj.getScoingb2()*0.3);
                double f12 = DoubleUtil.douToTwo(s12);

                double s12s=(obj.getScoingb2s()*0.2);
                double f12s = DoubleUtil.douToTwo(s12s);
                String b=String.valueOf(f12)+"/"+(f12s);

                double s13=(obj.getScoingb3()*0.3);
                double f13 = DoubleUtil.douToTwo(s13);

                double s13s=(obj.getScoingb3s()*0.2);
                double f13s = DoubleUtil.douToTwo(s13s);
                String c=String.valueOf(f13)+"/"+(f13s);




                obj.setScoreA(a);
                obj.setScoreB(b);
                obj.setScoreC(c);
             //   obj.setScoreD(d);
            //    obj.setScoreE(e);
                double s1=((obj.getScoingb1()+obj.getScoingb2()+obj.getScoingb3())*0.3)/3;
                double s2=((obj.getScoingb1s()+obj.getScoingb2s()+obj.getScoingb3s())*0.2)/3;
                BigDecimal bg = new BigDecimal(s1+s2);
                double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

                obj.setScore(f1);

                list.add(obj);
            }else {
                ExaminationResultsComDO obj = new ExaminationResultsComDO();
               // obj.setNum2s(stuSignUpComDO.getRealityNm2().toString());
                obj.setNum2s(stuSignUpComDO.getRealityNm2()!=null?stuSignUpComDO.getRealityNm2().toString():null);
                obj.setName(stuSignUpComDO.getName());
                obj.setUnitName(stuSignUpComDO.getUnitName());
                obj.setIdNumber(stuSignUpComDO.getIdNumber());
                obj.setTicketNumber(stuSignUpComDO.getTicketNumber());
                obj.setScoingb1(0.0);
                obj.setScoingb2(0.0);
                obj.setScoingb3(0.0);
                obj.setScore(0.0);
                obj.setScoreA("0/0");
                obj.setScoreB("0/0");
                obj.setScoreC("0/0");
                obj.setScoreD("0/0");
                obj.setScoreE("0/0");
                list.add(obj);

            }
        }

        page.setRecords(list);
        return Result.ok(page);
    }





}
